package com.yuanlrc.hotel.news.controller;

import java.util.List;

import com.yuanlrc.framework.util.ShiroUtils;
import com.yuanlrc.system.domain.SysUser;
import com.yuanlrc.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuanlrc.common.annotation.Log;
import com.yuanlrc.common.enums.BusinessType;
import com.yuanlrc.hotel.news.domain.News;
import com.yuanlrc.hotel.news.service.INewsService;
import com.yuanlrc.common.core.controller.BaseController;
import com.yuanlrc.common.core.domain.AjaxResult;
import com.yuanlrc.common.utils.poi.ExcelUtil;
import com.yuanlrc.common.core.page.TableDataInfo;

/**
 * 新闻管理Controller
 * 
 * @author yuanlrc
 * @date 2020-11-19
 */
@Controller
@RequestMapping("/hotel/news")
public class NewsController extends BaseController
{
    private String prefix = "hotel/news";

    @Autowired
    private INewsService newsService;
    @Autowired
    private ISysUserService userService;
    @RequiresPermissions("hotel:news:view")
    @GetMapping()
    public String news()
    {
        return prefix + "/news";
    }

    /**
     * 查询新闻管理列表
     */
    @RequiresPermissions("hotel:news:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(News news) {
        startPage();
        List<News> list = newsService.selectNewsList(news);
        for (News news1 : list) {
            SysUser sysUser = userService.selectUserById(news1.getAuthor());
            news1.setUser(sysUser);
        }
        return getDataTable(list);
    }

    /**
     * 导出新闻管理列表
     */
    @RequiresPermissions("hotel:news:export")
    @Log(title = "新闻管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(News news) {
        List<News> list = newsService.selectNewsList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 新增新闻管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻管理
     */
    @RequiresPermissions("hotel:news:add")
    @Log(title = "新闻管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(News news) {
        News news1 = newsService.selectNewsByTotal(news.getTotal());
        if (news1!=null){
            return error("新闻标题不能重复!");
        }
        SysUser sysUser = ShiroUtils.getSysUser();
        news.setUser(sysUser);
        news.setAuthor(sysUser.getUserId());
        //如果获取到后台登录用户的id异常 就返回false
        if (news.getAuthor()==null||news.getAuthor()==0){
            return toAjax(false);
        }
        if (news.getContent().length()<52){
            return error("新闻内容过短");
        }else if (news.getTotal().length()==0||news.getTotal()==null||news.getTotal()==" "){
            return error("标题不能为空");
        }
        int count = 0;
        try {
            count = newsService.insertNews(news);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }

    /**
     * 修改新闻管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        News news = newsService.selectNewsById(id);
        mmap.put("news", news);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻管理
     */
    @RequiresPermissions("hotel:news:edit")
    @Log(title = "新闻管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(News news) {
        if (news.getId()==null||news.getId()==0){
            return error("编辑失败");
        }
        if (news.getTotal().length()==0||news.getTotal()==null||news.getTotal()==" "){
            return error("新闻标题不能为空");
        }else if (news.getContent().length()<52){
            return error("新闻内容过短");
        }
        int count = 0;
        try {
            if (news.getAuthor()==null||news.getAuthor()==0){
                return toAjax(false);
            }
            count = newsService.updateNews(news);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }

    /**
     * 删除新闻管理
     */
    @RequiresPermissions("hotel:news:remove")
    @Log(title = "新闻管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int count = 0;
        try{
            count=newsService.deleteNewsByIds(ids);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }
}
